package com.malty.mall.controller.ums;

import com.malty.mall.common.CommonResult;
import com.malty.mall.config.security.component.DynamicSecurityMetadataSource;
import com.malty.mall.domain.dto.req.UmsResourceParam;
import com.malty.mall.service.ums.IUmsResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author malty
 */
@RestController
@Api(tags = "资源管理")
@RequestMapping("/resource")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UmsResourceController {
    private final IUmsResourceService umsResourceService;
    @Autowired
    private  DynamicSecurityMetadataSource metadataSource;

    @PostMapping("/create")
    @ApiOperation("创建资源")
    public CommonResult<Boolean> createUmsResource(@Validated @RequestBody UmsResourceParam umsResourceParam) {
        metadataSource.clearDataSource();
        return CommonResult.success(umsResourceService.create(umsResourceParam));
    }

    @GetMapping("/list")
    @ApiOperation("查询所有资源")
    public CommonResult<List<UmsResourceParam>> listUmsResource() {
        return CommonResult.success(umsResourceService.listAll());
    }

    @ApiOperation("根据id查询资源")
    @GetMapping("/list_by_role_id")
    public CommonResult<List<UmsResourceParam>> listUmsResourceByRoleId(@RequestParam Integer roleId) {
        return CommonResult.success(umsResourceService.listByRole(roleId));
    }

    @ApiOperation("根据id删除后台资源")
    @PostMapping("/delete_by_role_id")
    public CommonResult<Boolean> deleteUmsResourceByRoleId(@RequestParam Integer id) {
        metadataSource.clearDataSource();
        return CommonResult.success(umsResourceService.deleteResourceById(id));
    }
}
